package pl.edu.agh.pros.proxy.view.info;

import java.util.Arrays;

import pl.edu.agh.pros.proxy.logic.dao.ProviderInfoDAO;
import pl.edu.agh.pros.proxy.model.ProviderInfo;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.BaseTheme;

public class GeneralInfoForm extends VerticalLayout {

	private static final long serialVersionUID = -3599197108147242008L;

	ProviderInfo provider;

	private static final String COMMON_FIELD_WIDTH = "12em";

	@SuppressWarnings("serial")
	public GeneralInfoForm(ProviderInfo providerInfo) {

		provider = providerInfo;
		BeanItem<ProviderInfo> providerItem = new BeanItem<ProviderInfo>(provider);

		// Create the Form
		final Form generalInfoForm = new Form();
		generalInfoForm.setCaption("General");
		generalInfoForm.setWriteThrough(false);
		generalInfoForm.setInvalidCommitted(false);

		generalInfoForm.setFormFieldFactory(new ProviderFieldFactory());
		generalInfoForm.setItemDataSource(providerItem);
		generalInfoForm.setVisibleItemProperties(Arrays.asList(new String[] { "name", "contactMail", "website" }));

		// Add form to layout
		addComponent(generalInfoForm);

		// The cancel / apply buttons
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);
		Button discardChanges = new Button("Discard changes", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				generalInfoForm.discard();
			}
		});
		discardChanges.setStyleName(BaseTheme.BUTTON_LINK);
		buttons.addComponent(discardChanges);
		buttons.setComponentAlignment(discardChanges, Alignment.MIDDLE_LEFT);

		Button apply = new Button("Save", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				try {
					generalInfoForm.commit();
					ProviderInfoDAO.getInstance().save(provider);
					getWindow().showNotification("Saved");
				} catch (Exception e) {
					// Ignored, we'll let the Form handle the errors
				}
			}
		});
		buttons.addComponent(apply);
		generalInfoForm.getFooter().addComponent(buttons);
		generalInfoForm.getFooter().setMargin(false, false, true, true);
	}

	@SuppressWarnings("serial")
	private class ProviderFieldFactory extends DefaultFieldFactory {
		@Override
		public Field createField(Item item, Object propertyId, Component uiContext) {

			if ("name".equals(propertyId)) {
				TextField tf = new TextField("Name");
				tf.setRequired(true);
				tf.setRequiredError("Please enter organization name");
				tf.setNullRepresentation("");
				tf.setWidth(COMMON_FIELD_WIDTH);
				return tf;
			} else if ("contactMail".equals(propertyId)) {
				TextField tf = new TextField("Mail");
				tf.setRequired(true);
				tf.setRequiredError("Please enter contact mail");
				tf.setNullRepresentation("");
				tf.setWidth(COMMON_FIELD_WIDTH);
				tf.addValidator(new EmailValidator("Please provide a proper mail"));
				return tf;
			} else if ("website".equals(propertyId)) {
				TextField tf = new TextField("Website");
				tf.setNullRepresentation("");
				tf.setWidth(COMMON_FIELD_WIDTH);
				return tf;
			}
			return null;
		}
	}

}